package Exercise;

import java.util.HashSet;
import java.util.Set;

/**
 * Description：
 * Author: zhangc
 * Date：2017/9/9 9:49
 */
public abstract class Observed {
    public abstract void update(String string);
}

class MyObserved extends Observed {

    @Override
    public void update(String string) {
        System.out.println("MyObserved" + "=====" + string);
    }
}


class MyObverable {

    private String str;

    private Set<Observed> set = new HashSet<>();

    public void addListener(Observed observed) {
        set.add(observed);
    }

    public void setChange(String string) {
        this.str = string;
        for (Observed observed : set) {
            observed.update(str);
        }
    }


    public static void main(String[] args) {
        MyObverable obverable = new MyObverable();
        Observed observed1 = new MyObserved();
        Observed observed2 = new MyObserved();
        obverable.addListener(observed1);
        obverable.addListener(observed2);
        obverable.setChange("zhangchao");
    }

}
